home *** CD-ROM | disk | FTP | other *** search
- BitWise, v1.0 BITWISE.DOC
- by Shawn Bayern
-
- ==================================================================
- Introduction:
-
- BitWise is a Turbo Pascal 7.0 unit that allows you to compress
- boolean data by a factor of 8. Turbo Pascal's "boolean" type uses
- 8 bits (1 byte) to store a value of either "TRUE" or "FALSE";
- however, while the boolean data type is fast and relatively
- efficient, only 1 bit is truly necessary to store a "boolean-like"
- value.
- The procedures and functions contained in the unit BitWise
- allow use you to use an array of bytes to store boolean values.
- Each byte contains 8 bits, and each of these bits can store an
- individual value. While some speed is sacrificed (since the data
- has to be found and decoded), an effective compression factor of 8
- is achieved. (While speed is sacrificed for some purposes, you
- might find that BitWise actually *saves* time in some cases; if
- data is being moved around a lot within your program, or if you
- need to deal with large amounts of data at once, BitWise can
- actually speed up some operations).
- BitWise is shareware. You may freely distribute it, and you
- may use it for any noncommercial, private (i.e., non-institutional)
- purposes. For commercial (including use in shareware products) or
- institutional usage, the $10 registration fee must be paid. The
- source code will be provided to all users who register.
-
- ==================================================================
- Disclaimer:
-
- You are using BITWISE.TPU at your own risk. I take no
- responsibility for the use or misuse of BitWise. No guarantee,
- expressed or implied, is made concerning the accuracy of this
- documentation or concerning the correct operation of the routines
- contained in BITWISE.TPU. If the routines contained in BITWISE.TPU
- do not work as expected, the author is not to be held responsible.
-
- ==================================================================
- Instructions:
-
- To use BitWise, you must first place the BITWISE.TPU unit in
- a directory where Turbo Pascal will find it. In every program that
- you plan to use BitWise, you must "use" it as follows:
-
- uses BitWise;
-
- For further instructions regarding unit usage, please see the
- Turbo Pascal documentation.
-
- Next, you must define an array of bytes to store your "pseudo-
- pseudo-boolean" data. This array should contain one-eight as many
- elements as the number of boolean values you wish to store.
- Consider the following examples:
- var
- MyArray: array[1..100] of byte; {stores 800 bits}
- MyArray2: array[1..2000] of byte; {stores 16000 bits}
-
- If you plan on using eight bits or fewer, you can simply
- define a "byte" value:
-
- var
- TestByte: byte;
-
- It is usually inadvisable to use BitWise for only eight bits;
- however, if you plan on moving the data extremely many times,
- BitWise might save you time.
-
- Now that you have the appropriate variables defined, you can
- begin to use the BitWise procedures and functions.
-
- The interface section of BITWISE.TPU is as follows (a detailed
- explanation of each procedure and function will follow this
- listing):
-
- *** interface
- ***
- *** type
- *** BitValue=0..1;
- ***
- *** procedure SetElement(var BigArray: array of byte; const Elem:
- *** longint);
- *** procedure ResetElement(var BigArray: array of byte; const Elem:
- *** longint);
- *** function Element(const BigArray: array of byte; const Elem:
- *** longint): boolean;
- *** procedure ToggleElement(var BigArray: array of byte; const
- *** Elem: longint);
- *** procedure AssignElement(var BigArray: array of byte; const
- *** Elem: longint; const ElemValue: BitValue);
- *** function BoolToBit(const BoolArray: array of boolean;
- *** var BitArray: array of byte): byte;
- *** function BitToBool(const BitArray: array of byte;
- *** var BoolArray: array of boolean): byte;
-
- Explanation:
-
- First, I'm going to explain how data is stored in your array
- of bytes; then, I'll show how each of these routines can be used.
-
- Let's consider an array[1..100] of byte. As I already
- explained, this array contains 800 bits. The procedures and
- functions in BitWise manipulate these bits directly. For the
- purposes of BitWise, it's best to think of these bits as being
- numbered from 1 to 800. More generally, if you're dealing with N
- bits, then these bits are numbered from 1 to N -- *regardless* of
- the numbers that you use to index your array.
-
- For instance, if you define
-
- var
- MyArray: array[2..5] of byte; {40 bits}
-
- then the 40 bits will still be numbered from 1 to 40. Again,
- BitWise doesn't care about the index numbers of your arrays. It
- looks upon all arrays as just a sequential series of bytes
- beginning with one byte and ending with another.
-
- ------------------------------
-
- Now, on to the discussion of the procedures and functions in
- BitWise. For the rest of this section, we're going to refer to the
- following array:
-
- var
- TestArray: array[1..100] of byte; {800 bits}
-
- Procedure SetElement can be used to "turn on" a bit in the
- array. If you want to turn on the 123rd bit, then you simply call
- SetElement as follows:
-
- SetElement(TestArray, 123);
-
- Now, bit #123 of TestArray has a value of "1."
-
- ------------------------------
-
- Procedure ResetElement "turns off" a bit in the array. If you
- want to turn off the 123rd bit, then you can call ResetElement as
- follows:
-
- ResetElement(TestArray, 123);
-
- Now, bit #123 of TestArray has a value of "0."
-
- ------------------------------
-
- If you wish to check whether an element is "on" or not, you
- can use the function Element as follows:
-
- If Element(TestArray, 123) then
- Writeln('Element 123 of TestArray is "on."');
-
- Note that the syntax of "Element" is designed to mimic the usual
- usage of a boolean type. If you were dealing with an array of
- boolean variables, you would use: "If BooleanArray[counter] then
- ..." The function, "Element," should make bitwise checking as
- simple as possible.
-
- ------------------------------
-
- If you want to toggle a bit in your array, use the
- ToggleElement procedure as follows:
-
- ToggleElement(TestArray, 123);
-
- If element #123 was "1," it is now "0," and vice-versa.
-
- ------------------------------
-
- While you can use SetElement, ResetElement, and ToggleElement
- to manipulate bits in your array, you might want to define a bit
- directly (i.e., "pass" a value to a bit). To do this, you can use
- AssignElement:
-
- AssignElement(TestArray, 123, 1); {bit #123 now equals 1}
- AssignElement(TestArray, 123, 0); {bit #123 now equals 0}
-
- You may have noticed the data type "BitValue" in the interface
- section of BITWISE.TPU. Not much needs to be said about it; it is
- used here only in the procedure AssignElement. It just checks to
- make sure that a valid value (a 1 or a 0) is being passed to the
- bit to which you're assigning a value.
-
- ------------------------------
-
- The procedures and functions just described give you complete
- control over the bits in your array. Two more functions are
- provided for flexibility; these functions allow you to convert your
- array of bytes into a standard boolean array (which is eight times
- large) and back. Note the following examples:
-
- uses BitWise;
-
- var
- TestArray: array[1..100] of byte; {800 bits}
- BigArray: array[1..800] of boolean;
-
- begin
- BitToBool(TestArray,BigArray);
- {this copies the data in TestArray to BigArray. For instance,
- if the first three bits in test array (bits #1, #2, and #3)
- are "1 0 1" respectively, then BigArray[1] becomes "True,"
- BigArray[2] becomes "False," and BigArray[3] becomes "True."}
- BoolToBit(BigArray,TestArray);
- {does the opposite procedure; if BigArray[1] is "true" then
- bit #1 of TestArray becomes "1"}
- end.
-
- NOTE: Since BitToBool and BoolToBit are functions, not procedures,
- this program will run only if Turbo Pascal's extended syntax is
- "on" {$X+}. Since X+ is the default, it shouldn't be a problem; it
- merely allows functions to be called as if they were procedures
- (and the return values are discarded).
- You can run BitToBool and BoolToBit as procedures, like in the
- example above. However, if your program is more complex and you
- want slight error checking, you might want to look at the return
- values of these two functions. Both BitToBool and BoolToBit return
- a "0" if the boolean array was exactly 8 times as large as the byte
- array (meaning that the target array is exactly large enough to
- contain the data in the source array). BitToBool and BoolToBit
- still work even if the boolean array is *not* exactly 8 times as
- large as the byte array; however, in this case, they return a "1."
- It's up to you to determine how you wish to use BitToBool and
- BoolToBit; they're pretty flexible, but they don't catch any
- errors, so be careful (and test your program to make sure that the
- data is copied in the way you expect, if the boolean array is not
- exactly 8 times larger than the byte array).
-
- ------------------------------
-
- Technical note: Within each byte, the bits are ordered as
- follows: #7 #6 #5 #4 #3 #2 #1 #8. For instance, if elements
- 1 through 8 are "1111 0000," then the first byte will read as
- follows: "0001 1110." Using these routines, you never have to
- worry about this order; however, you might wish to know it if you
- plan to write your own support routines.
- There's no particular reason for this order; it just allowed
- the algorithm that I was using to work more quickly than some other
- orders. I originally tried "1 2 3 4 5 6 7 8," but that was
- slightly slower.
-
- ==================================================================
- Registration:
-
- As I mentioned above, the registration fee is $10. If you
- wish to register, please send checks made out to "Shawn Bayern" to:
-
- Shawn Bayern
- 5 Cedarwood Court
- Laurel Hollow, NY 11791.
-
- My phone number is (516)-367-9038, and my e-mail address is
- "bayern@cshl.org" if you wish to contact me.
-
- All registered users will receive a copy of the source code to
- BITWISE.TPU. If you use BitWise, please support the shareware
- concept and register. (If you're using BitWise in a commercial or
- institutional environment, you have a legal obligation to regis-
- ter.)
-
- ==================================================================
- Copyright Notice:
-
- BitWise--including BITWISE.TPU and this documentation file--are
- copyright 1995, Shawn Bayern. Permission is granted to freely
- distribute this file provided that neither BITWISE.DOC nor
- BITWISE.TPU are modified in any way.
- It is prohibited to disassemble or otherwise reverse-engineer
- BITWISE.TPU, or to remove the BitWise copyright notice from
- programs that use BitWise. (Programs that use bitwise will contain
- a brief copyright message somewhere within them; a tiny bit of code
- to check that this copyright message has not been altered is
- included as well).